*************************************************************************
*********************** Putting Datasets Together *********************** 
*************************************************************************
*************************************************************************

global beg_path `" XXX "' // Indicate your working directory

use `"${beg_path}Data\Macro\Base_dataset.dta"', replace

*Adding affluent generations 
merge m:1 country year using `"${beg_path}Data\Macro\share_affluent_and_GDP.dta"' 
drop _merge  

*Adding cohorts socialised into voting at 18 
merge m:1 country year using `"${beg_path}Data\Macro\voting_age18_shares.dta"'
drop _merge 

*Adding cohorts born after 1959
merge m:1 country year using `"${beg_path}Data\Macro\share_after_1959.dta"'
drop _merge

save `"${beg_path}Data\Macro\Macro_dataset.dta"', replace


********************************************************
*********************** FOR CSES *********************** 
********************************************************
********************************************************

gen country_CSES =. 
	replace country_CSES = 1 if country == 1	//	Albania
	replace country_CSES = 2 if country == 2	//	Argentina
	replace country_CSES = 3 if country == 4	//	Australia
	replace country_CSES = 4 if country == 5	//	Austria
	replace country_CSES = 5 if country == 7	//	Belarus
	replace country_CSES = 6 if country == 8	//	Belgium
	replace country_CSES = 7 if country == 12	//	Brazil
	replace country_CSES = 8 if country == 13	//	Bulgaria
	replace country_CSES = 9 if country == 16	//	Canada
	replace country_CSES = 10 if country == 18	//	Chile
	replace country_CSES = 11 if country == 22 //	Croatia
	replace country_CSES = 12 if country ==24	//	Czech Republic
	replace country_CSES = 13 if country == 26	//	Denmark
	replace country_CSES = 14 if country == 31	//	Estonia
	replace country_CSES = 15 if country == 33	//	Finland
	replace country_CSES = 16 if country == 34	//	France
	replace country_CSES = 17 if country == 37	//	Germany
	replace country_CSES = 19 if country == 39	//	Greece
	replace country_CSES = 21 if country == 45	//	Hungary
	replace country_CSES = 23 if country == 49	//	Ireland
	replace country_CSES = 24 if country == 50	//	Israel
	replace country_CSES = 25 if country == 51	//	Italy
	replace country_CSES = 26 if country == 53	//	Japan
	replace country_CSES = 27 if country == 54	//	Kenya
	replace country_CSES = 40 if country == 55	//	Korea South
	replace country_CSES = 28 if country == 57	//	Kyrgyzstan
	replace country_CSES = 29 if country == 59	//	Latvia
	replace country_CSES = 30 if country == 63	//	Lithuania
	replace country_CSES = 31 if country == 71	//	Mexico
	replace country_CSES = 32 if country == 74	//	Montenegro
	replace country_CSES = 33 if country == 78	//	Netherlands
	replace country_CSES = 34 if country == 79	//	New Zealand
	replace country_CSES = 35 if country == 83	//	Norway
	replace country_CSES = 36 if country == 87	//	Peru
	replace country_CSES = 37 if country == 88	//	Philippines
	replace country_CSES = 38 if country == 89	//	Poland
	replace country_CSES = 39 if country == 90	//	Portugal
	replace country_CSES = 41 if country == 91	//	Romania
	replace country_CSES = 42 if country == 92	//	Russia
	replace country_CSES = 43 if country == 94	//	Serbia
	replace country_CSES = 44 if country == 98	//	Slovak Republic
	replace country_CSES = 45 if country == 99	//	Slovenia
	replace country_CSES = 46 if country == 102 //	South Africa
	replace country_CSES = 47 if country == 103 //	Spain
	replace country_CSES = 48 if country == 106 //	Sweden
	replace country_CSES = 49 if country == 107 //	Switzerland
	replace country_CSES = 50 if country == 109 //	Taiwan 
	replace country_CSES = 51 if country == 110 //	Thailand
	replace country_CSES = 52 if country == 113 //	Turkey
	replace country_CSES = 53 if country == 115 //	Ukraine
	replace country_CSES = 18 if country == 116 //	United Kingdom
	replace country_CSES = 54 if country == 117 //	United States
	replace country_CSES = 55 if country == 118 //	Uruguay

drop if country_CSES  ==. 

preserve 

collapse starting_year_18, by(country_CSES year) // 

save `"${beg_path}Data\Micro\starting_year_18_CSES.dta"', replace

restore


// Local level
gen local_l = Vdem_v2ellocelc > 0
// Regional level 
gen regional_l = Vdem_v2elsrgel > 0 
// EU level 
gen eu = year > 1978 & inlist(country, 8, 26, 34, 37, 51, 116, 49, 64, 78)
replace eu = 1 if year > 1980 & inlist(country, 39) // greece
replace eu = 1 if year > 1985 & inlist(country, 103, 90)
replace eu = 1 if year > 2003 & inlist(country, 24, 31, 45, 59, 63, 89, 98, 99, 23)
replace eu = 1 if year > 1994 & inlist(country, 106, 33, 5)
replace eu = 1 if year > 2006 & inlist(country, 13, 91)
replace eu = 1 if year > 2012 & inlist(country, 22)
// Bicameralism 
gen bicam = Vdem_v2lgbicam > 1
// Referendum 
egen ref_yes_or_no = rowtotal(Vdem_v2ddyrci_4 Vdem_v2ddyrci_3 Vdem_v2ddyrci_2 Vdem_v2ddyrci_1 ///
Vdem_v2ddyror_4 Vdem_v2ddyror_3 Vdem_v2ddyror_2 Vdem_v2ddyror_1 Vdem_v2ddyrrf_4 Vdem_v2ddyrrf_3 ///
Vdem_v2ddyrrf_2 Vdem_v2ddyrrf_1 Vdem_v2ddyrpl_4 Vdem_v2ddyrpl_3 Vdem_v2ddyrpl_2 Vdem_v2ddyrpl_1)
recode ref_yes_or_no (0=0) (1/max=1) (.=.), gen(referendum) 

*Generating the number of electoral institutions 
egen temp1 = rowtotal(referendum local_l regional_l eu bicam Vdem_v2ex_elechos)

gen N_el_instit  = temp1/6 // to make the variable vary between 0 and 1
lab var N_el_instit "Elective Institutions" 
drop temp1


keep country_CSES year EL_TYPE multiple_el_id  Majority Closeness Concurrent CV N_el_instit 

save `"${beg_path}Data\Micro\contextual_data_for_CSES.dta"', replace
